/*******************************************************************************
 * Copyright (c) 2011 EclipseDevX.org and others.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at:
 * 
 *     http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipsedevx.metrics.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.eclipsedevx.metrics.DevXMetricsPlugin;

/**
 * 
 * 
 * @author dcarew
 */
public class DayCode {
	private String code;
	private Date date;
	
	public DayCode(String code) {
		this.code = code;
		
		try {
			this.date = df.parse(code);
		} catch (ParseException pe) {
			DevXMetricsPlugin.logError(pe);
		}
	}
	
	public DayCode(Date date) {
		this.date = date;
		this.code = df.format(date);
	}
	
    public String getCode() {
		return code;
	}

	public Date getDate() {
		return date;
	}

	public DayCode next() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, 1);
		return new DayCode(cal.getTime());
	}
	
	public DayCode prev() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		return new DayCode(cal.getTime());
	}

	private static DateFormat df = new SimpleDateFormat("yyyyMMdd");
    
    public static DayCode today() {
        return new DayCode(new Date());
    }
	
}
